import { createSlice } from '@reduxjs/toolkit'
import globalConfig from '@/pages/bi-core/Panel/Global/global'
import { configsTodefaultValue } from '@/utils/core'

export const defaultQuery = {
  aggrMode: 'sum', // sum: 聚合 detail: 明细
  datasetId: null,
  limit: 20, // 结果展示条数
  queryFields: {}, //数据建模
  drill: {} //下钻配置
}

export const coreSlice = createSlice({
  name: 'app',
  initialState: {
    system: {},
    selectedWidgetIds: [],
    widgets: {},
    layouts: {},
    globalValue: configsTodefaultValue(globalConfig), // 全局配置
    delWidgets: {}
  },
  reducers: {
    changeSystem: (state, action) => {
      state.system = action.payload
    },
    changeSelectedWidgetIds: (state, action) => {
      state.selectedWidgetIds = action.payload
    },
    changeWidgets: (state, action) => {
      state.widgets = action.payload
    },
    changeLayouts: (state, action) => {
      state.layouts = action.payload
    },
    changeGlobalValue: (state, action) => {
      state.globalValue = action.payload
    },
    changeDelWidgets: (state, action) => {
      state.delWidgets = action.payload
    }
  }
})

export const {
  changeSystem,
  changeSelectedWidgetIds,
  changeWidgets,
  changeLayouts,
  changeGlobalValue,
  changeDelWidgets
} = coreSlice.actions

export default coreSlice.reducer
